package com.tangguo.vshop.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.tangguo.vshop.domain.Order;
import com.tangguo.vshop.domain.OrderItem;
import com.tangguo.vshop.domain.Shoppingcart;
import com.tangguo.vshop.repository.CustomerRepository;
import com.tangguo.vshop.repository.OrderRepository;
import com.tangguo.vshop.repository.ProductRepository;
import com.tangguo.vshop.repository.ShoppingCartRepository;
import com.tangguo.vshop.repository.UserRepository;
import com.tangguo.vshop.security.JwtUser;

@RestController
@RequestMapping("/shoppingcart")
public class ShoppingcartController {
	@Autowired
	private ProductRepository productRepository;
	@Autowired
	private ShoppingCartRepository shoppingCartRepository;
	@Autowired
	private UserRepository userRepository;
	@Autowired
	private OrderRepository orderRepository;
	@Autowired
	private CustomerRepository customerRepository;
	@RequestMapping("/add")
	public ResponseResult add(@RequestBody OrderItem orderItem){
		JwtUser jwtUser = (JwtUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		Shoppingcart cart = shoppingCartRepository.findByCreatorAndCustomer(jwtUser.getId(),orderItem.getCustomer().getId());
		orderItem.setProduct(productRepository.findOne(orderItem.getProduct().getId()));
		if(cart == null){
			cart = new Shoppingcart();
			cart.setCreator(userRepository.findOne(jwtUser.getId()));
			cart.setCustomer(customerRepository.findOne(orderItem.getCustomer().getId()));
			cart.setOrderDetails(new ArrayList<>());
			cart.getOrderDetails().add(orderItem);
			cart.calculateTotalAmount();
			shoppingCartRepository.insert(cart);
		}else{
			List<OrderItem>orderDetails = cart.getOrderDetails();
			OrderItem item = getOrderItem(orderDetails,orderItem.getProduct().getId());
			if(item!=null){
				item.setPrice(orderItem.getPrice());
				item.setQty(orderItem.getQty());
			}else{
				cart.getOrderDetails().add(orderItem);
			}
			cart.calculateTotalAmount();
			shoppingCartRepository.save(cart);
		}

		return ResponseResult.ok("添加成功");
	}

	@RequestMapping("")
	public List<Shoppingcart> carts(){
		JwtUser jwtUser = (JwtUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		return  shoppingCartRepository.findByCreator(jwtUser.getId());

	}
	@RequestMapping("cart")
	public Shoppingcart cart(@RequestParam("customerId")String customerId){
		JwtUser jwtUser = (JwtUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		return  shoppingCartRepository.findByCreatorAndCustomer(jwtUser.getId(),customerId);

	}
	@RequestMapping(value="makeOrder",method=RequestMethod.POST)  
	public ResponseResult saveOder(){
		JwtUser jwtUser = (JwtUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		List<Shoppingcart> carts = shoppingCartRepository.findByCreator(jwtUser.getId());
		if(carts.isEmpty()){
			return ResponseResult.error("购物车为空");
		}
		for(Shoppingcart cart:carts){
			if(cart.getOrderDetails().isEmpty()){
				continue;
			}
			Order order = new Order();
			order.setCustomer(cart.getCustomer());
			order.setMainbody(cart.getMainbody());
			order.setCreateTime(new Date());
			order.setCode("1111111111111");
			order.setCreator(userRepository.findOne(jwtUser.getId()));
			order.setOrderDetails(cart.getOrderDetails());
			order.calculateTotalAmount();
			orderRepository.save(order);
		}

		//清空购物车
		shoppingCartRepository.delete(carts);
		return ResponseResult.ok("保存成功");
	}

	@RequestMapping(value="clearAll",method=RequestMethod.POST)  
	public ResponseResult clearAll(){
		JwtUser jwtUser = (JwtUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		List<Shoppingcart> carts = shoppingCartRepository.findByCreator(jwtUser.getId());
		shoppingCartRepository.delete(carts);
		return ResponseResult.ok("清空成功");
	}

	@RequestMapping(value="clear",method=RequestMethod.POST)  
	public ResponseResult clear(@RequestBody Shoppingcart cart){
		shoppingCartRepository.delete(cart);
		return ResponseResult.ok("清空成功");
	}

	private OrderItem getOrderItem(List<OrderItem> orderDetails,String productId) {
		for(OrderItem item:orderDetails){
			if(item.getProduct().getId().equals(productId)){
				return item;
			}
		}
		return null;
	}

}
